// pages/diet/diet.js
Page({
  data: {
    searchValue: '',
    categories: [
      { name: '谷物', image: '/images/diet-images/grain.jpg' },
      { name: '应季水果', image: '/images/diet-images/fruit.jpg' },
      { name: '绿色蔬菜', image: '/images/diet-images/vegetable.jpg'},
      { name: '豆类', image: '/images/diet-images/bean.jpg' }
    ],
    rankingList: [
      { id: 1, name: '冰糖雪梨', image: '/images/diet-images/food1.jpg' , efficacy:'滋阴润肺'},
      { id: 2, name: '绿豆汤', image: '/images/diet-images/food2.jpg'  , efficacy:'消暑祛湿'},
      { id: 3, name: '四神汤', image: '/images/diet-images/food3.jpg' , efficacy:'健脾养胃' }
    ],
    foodList: [
      { id: 1, name: '茼蒿', benefits: '清热、开胃', imgUrl: '/images/diet-images/veg1.jpg', favorite: false },
      { id: 2, name: '西兰花', benefits: '增强免疫力', imgUrl: '/images/diet-images/veg2.jpg', favorite: true },
      { id: 3, name: '苋菜', benefits: '补铁', imgUrl: '/images/diet-images/veg3.jpg', favorite: false },
      { id: 4, name: '地瓜叶', benefits: '抗氧化', imgUrl: '/images/diet-images/veg4.jpg', favorite: false }
    ]
  },
// 1.搜索框处理
  // onSearchInput(e) {
  //   this.setData({
  //     searchValue: e.detail.value
  //   });
  // },
  // onSearch() {
  //   console.log('搜索内容：', this.data.searchValue);
  // },

gridTap(e) {
    const index = e.currentTarget.dataset.index; 
    const selectedItem = this.data.categories[index];  
    wx.navigateTo({
      url: `/foodLists/pages/foodList/foodList?category=${selectedItem.name}`,
    });
},

// 2.查看全部分类
  viewAllCategories: function() {
    // 这里处理查看全部分类的逻辑，例如导航到新页面
    wx.navigateTo({
      url: '/foodLists/pages/category/category' 
    });
  },
 //3.查看全部排行榜
  viewMoreRanking() {
    // wx.showToast({
    //   title: '查看更多',
    //   icon: 'none'
    // });
    wx.navigateTo({
      url: '/foodLists/pages/rank/rank' 
    });
  },
 //4.查看全部食物详细信息
 viewMoreFood: function() {
  wx.navigateTo({
    url: '/foodLists/pages/foods/foods' 
  });
},
 //5.收藏
  toggleFavorite(e) {
    const id = e.currentTarget.dataset.id;
    const newList = this.data.foodList.map(item => {
      if (item.id === id) {
        item.favorite = !item.favorite;
      }
      return item;
    });
    this.setData({ foodList: newList });
  
   // 提取收藏的食物列表
   const favoriteFoodList = newList.filter(item => item.favorite);
   // 保存收藏的食物列表到本地存储
   wx.setStorageSync('favoriteFoodList', favoriteFoodList);
   console.log("详细信息：",favoriteFoodList)
  }
});

